<?php

defined('BASEPATH') OR exit('No direct script access allowed');

class Common_String
{
	public function random($length = 10, $char = false)
	{
		$str = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
		if(!$char)
		{
			$str = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ?/!@';
		}
		$salt = '';
		for($i=0 ; $i<$length ; $i++)
		{
			$salt .= $str[rand(0, strlen($str) - 1)];
		}
		return $salt;
	}
	
	public function encode_password($salt, $password)
	{
		return md5($salt.md5($salt.md5($password).$salt).$salt);
	}
	
	public function encode_cookie($cookie)
	{
		return $this->random(10).base64_encode($cookie);
	}
	
	public function decode_cookie($cookie)
	{
		return base64_decode(substr($cookie, 10));
	}
	
	public function allow_post($para, $allow)
	{
		$temp = "";
		if(isset($para) && count($para) && isset($allow) && count($allow))
		{
			foreach($para as $key => $val)
			{
				if(in_array($key, $allow))
				{
					$temp[$key] = $val;
				}
			}
			return $temp;
		}
		return $para;
	}
	
	public function php_redirect($url)
	{
		header('Location: '. $url);
		die;	
	}
	
	public function js_redirect($alert, $url)
	{
		die('<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /><script type="text/javascript">alert(\''.$alert.'\'); location.href = \''.$url.'\' </script>');	
	}
}